package com.csw.mysqldate.fegin;

import com.alibaba.fastjson.JSONObject;
import com.csw.mysqldate.interceptor.traceId.TraceIdFilter;
import com.csw.mysqldate.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/mysqldate/testfegin")
public class TestFeginController {
    @Autowired
    private TestService testService;

    @GetMapping(value = "/none")
    public Result none() {
        //http://localhost:8080/mysqldate/testfegin/none
        log.info("none-test-success");
        testService.noneReceive();

        /**
         *   return new Request.Options(6 * 1000, 11 * 1000);
         *
         *  log.info("none");
         *         TimeUnit.SECONDS.sleep(10);
         *         log.info("none-success");
         *
         * 2022-10-24 22:03:36.887  INFO 14660 --- [nio-8080-exec-3] com.csw.mysqldate.fegin.ReceiveFegin     : none
         * 2022-10-24 22:03:46.900  INFO 14660 --- [nio-8080-exec-3] com.csw.mysqldate.fegin.ReceiveFegin     : none-success
         * 2022-10-24 22:03:46.923  INFO 14660 --- [nio-8080-exec-1] com.csw.mysqldate.fegin.TestFegin        : none-test-success
         */
        return Result.ok();
    }

    @GetMapping(value = "/idd")
    public Result id() {
        //http://localhost:8080/mysqldate/testfegin/idd
        testService.testIdReceive("666");
        log.info("idd-test-success");
        return Result.ok();
    }

    @PostMapping(value = "/object")
    public Result object() {
        log.info("object-test-success-0");
        //http://localhost:8080/mysqldate/testfegin/object
        JSONObject object = new JSONObject();
        object.put("aa", "aa");
        object.put("bb", "bb");
        testService.testObjectReceive(object);
        log.info("object-test-success-1");
        return Result.ok();
    }


    @GetMapping(value = "/noneReceive")
    public void noneReceive() throws InterruptedException {
        //http://localhost:8080/mysqldate/testfegin/none
        log.info("none-test-success-receive");
        TimeUnit.SECONDS.sleep(1);
    }

    @GetMapping(value = "/iddReceive")
    public Result idReceive(@RequestParam("id") String id) throws InterruptedException {
        //http://localhost:8080/mysqldate/testfegin/idd
        log.info("idd-test-success-receive");
        TimeUnit.SECONDS.sleep(1);
        return Result.ok();
    }

    @PostMapping(value = "/objectReceive")
    public void objectReceive(@RequestBody JSONObject jsonObject) throws InterruptedException {
        log.info("object-test-success-0-receive");
        //http://localhost:8080/mysqldate/testfegin/object
        JSONObject object = new JSONObject();
        object.put("aa", "aa");
        object.put("bb", "bb");
        log.info("object-test-success-1-receive");
        TimeUnit.SECONDS.sleep(1);
    }

    @GetMapping("futureTest")
    public void futureTest() throws ExecutionException, InterruptedException {
        int aa = 10;
        String requestId = TraceIdFilter.getRequestId();
        for (int i = 0; i < aa; i++) {
            CompletableFuture future = CompletableFuture.runAsync(() -> {
                MDC.put("traceId", requestId);
                log.info("aa");
            });
            future.get();
        }
    }

    @GetMapping("mutiplyMDC")
    public void mutiplyMDC() throws ExecutionException, InterruptedException {
        Random random = new Random();
        int nexted = random.nextInt(100);
        log.info("aa:{}", nexted);
        int i = 1 / 0;
        int aa = nexted;
        log.info("bb:{}", nexted);

    }


}
